package com.yuanqi.dz.dc.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import pnbclient.helper.StringHelper;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;

import com.yuanqi.dz.dc.info.DZFieldConfig;
import com.yuanqi.dz.dc.info.MainConfig;
import com.yuanqi.dz.dc.info.MerchantConfig;


public class TextValidateUtil {

	//public static Map<String,Item[]> validate=new HashMap<String,Item[]>();
	public static Map<String, Map<String, MerchantConfig>>   config=new HashMap<String, Map<String,MerchantConfig>>();
	public static final PnbLogging LOG = PnbLogFactory.getLogger("DZ_LOG_EXT");
	static {
		initParseFactory();
	}

	public final static void initParseFactory() {
		DocumentBuilderFactory docfact = DocumentBuilderFactory.newInstance();
		DocumentBuilder docb;
		try {
			docb = docfact.newDocumentBuilder();
			Document doc;
			doc = docb.parse(TextValidateUtil.class.getClassLoader().getResourceAsStream("dz.xml"));
			Element root = doc.getDocumentElement();
			NodeList interfaceNodes = root.getElementsByTagName("Interface");
			int  interfaceNum  = interfaceNodes.getLength();
			//遍历接口
			for ( int i = 0 ;i<interfaceNum;i++) {
				/*if(i==2){
					System.out.println(i);
				}*/
				Element element = (Element) interfaceNodes.item(i);
				NodeList merchantNodes = element.getElementsByTagName("merchant");
				Map<String , MerchantConfig>  mapm=new HashMap<String, MerchantConfig>();
				//遍历商户号
				for (int j = 0; j < merchantNodes.getLength(); j++) {
					MainConfig mainConfig=new MainConfig();
					MerchantConfig config=new MerchantConfig();
					Element   e2=(Element) merchantNodes.item(j);
					NodeList Fields = e2.getElementsByTagName("Fields");
					NodeList mains = e2.getElementsByTagName("Mains");
					Element  mainEle=(Element) mains.item(0);
					Element  dzfieldEle=(Element) Fields.item(0);
					Element  S3dzfieldEle = (Element) Fields.item(1);
					if(null!=mainEle){
						mainConfig=getMainConfig(mainEle);
						mainConfig.setMerchantNum(e2.getAttribute("merchant_num"));
						config.setMainConfig(mainConfig);
					}
					Map<String, DZFieldConfig> s1map = new HashMap<String, DZFieldConfig>();
					Map<String, DZFieldConfig> s2map = new HashMap<String, DZFieldConfig>();
					if(null!=dzfieldEle){
						
						s1map=getDzFiled(dzfieldEle,s1map);
						
					}
					if(null!= S3dzfieldEle){
						s2map=getDzFiled(S3dzfieldEle,s2map);
					}
					Map<String, Map<String,DZFieldConfig>> map= new  HashMap<String, Map<String,DZFieldConfig>>();
					map.put("S1", s1map);
					map.put("S3", s2map);
					config.setMap(map);
					mapm.put(e2.getAttribute("merchant_num"), config);
				}
				config.put(element.getAttribute("type"), mapm);
			}
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}



	}
	private static  String getIsNull(String str){
		if(null==str){
			str="";
		}else{
			str=str.trim();
		}
		return str;

	} 

	public static void main(String[] args) {
		TextValidateUtil.initParseFactory();
		System.out.println(config.get("msdc").get("common")); 
		System.out.println("##########");
		System.out.println(config);
	} 


	public  static MainConfig  getMainConfig(Element  maincon){
		NodeList   mainNodes=   maincon.getElementsByTagName("Field");
		Element  ele=null;
		MainConfig mainConfig=new MainConfig();
		for (int k = 0; k < mainNodes.getLength(); k++) {
			ele=(Element)mainNodes.item(k);
			String type=ele.getAttribute("type");
			String value=ele.getAttribute("value");
			if("local_path".equals(type)){
				mainConfig.setLocalPath(value);
			}else if("s1_local_path".equals(type)){
				mainConfig.setS1LocalPath(value);
			}else if("s3_local_path".equals(type)){
				mainConfig.setS3LocalPath(value);
			}else if("source_type".equals(type)){
				mainConfig.setSourceType(getIsNull(value));
			}else if("ftp_ip".equals(type)){
				mainConfig.setFtpIp(value);
			}else if("ftp_port".equals(type)){
				mainConfig.setFtpPort(StringHelper.isEmpty(value)?21:Integer.valueOf(value));
			}else if("ftp_username".equals(type)){
				mainConfig.setFtpUsername(getIsNull(value));
			}else if("ftp_pwd".equals(type)){
				mainConfig.setFtpPwd(getIsNull(value));
			}else if("start_row".equals(type)){
				mainConfig.setStartRow(StringHelper.isEmpty(value)?1:Integer.valueOf(value));
			}else if("splitchar".equals(type)){
				mainConfig.setSplitchar(value);
			}else if("is_escape".equals(type)){
				mainConfig.setIsEscape(StringHelper.isEmpty(value)?1:Integer.valueOf(value));
			}else if("dz_date_format".equals(type)){
				mainConfig.setDzDateFormat(StringHelper.isEmpty(value)?"yyyyMMdd":getIsNull(value));
			}else if("dz_date_addday".equals(type)){
				mainConfig.setDzDateAddday(StringHelper.isEmpty(value)?"-1":value);
			}else if("charset".equals(type)){
				mainConfig.setCharset(getIsNull(value));
			}else if("filename".equals(type)){
				mainConfig.setFileName(value);
			}else if("s1_filename".equals(type)){
				mainConfig.setS1FileName(value);
			}else if("s3_filename".equals(type)){
				mainConfig.setS3FileName(value);
			}
		}
		return mainConfig;

	}

	public static  Map<String, DZFieldConfig>  getDzFiled(Element e3,Map<String, DZFieldConfig> map){
		
		try {
			NodeList Fields = e3.getElementsByTagName("Field");
			String  type = e3.getAttribute("type");
			Element fieldElement = null;
			DZFieldConfig field = null;
			DZFieldConfig[] items = new DZFieldConfig[Fields.getLength()];

			//遍历字段数据
			for (int m = 0; m< items.length; m++) {
				fieldElement = (Element) Fields.item(m);
				field = new DZFieldConfig();
				field.setFieldName(fieldElement.getAttribute("type"));
				String  is_join = fieldElement.getAttribute("is_join");
				field.setIs_join(StringHelper.isEmpty(is_join)?0:Integer.valueOf(is_join));
				if(null != is_join  && "1".equals(is_join)){
					if(null != type && "1".equals(type)){
						field.setStart_index(Integer.valueOf(fieldElement.getAttribute("start_index")));
						field.setEnd_index(Integer.valueOf(fieldElement.getAttribute("end_index")));
					}else if("2".equals(type)){
						field.setJoinOrder(Integer.valueOf(fieldElement.getAttribute("index")));
					}
				}
				items[m] = field;
				map.put(fieldElement.getAttribute("type"), field);
			}
			return  map;
		} catch (Exception e) {
			LOG.addError(e);
			return  map;
		}


	}

}
